/**
 * 
 */
package com.zxc.disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadFactory;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import cn.hutool.core.thread.NamedThreadFactory;

/**
 * @author admin
 *
 */
public class DisruptorDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		method1();
		
		
	}

	public static void method2(){
		// Executor that will be used to construct new threads for consumers 
		ThreadFactory threadFactory =  new NamedThreadFactory("DisruptorDemo Thread",false);
        
		// Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;// Construct the Disruptor 
        
        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, threadFactory);
        
        // 可以使用lambda来注册一个EventHandler 
        disruptor.handleEventsWith((event, sequence, endOfBatch) -> System.out.println("Event: " + event.getValue()));
        
        // Start the Disruptor, starts all threads running 
        disruptor.start();
        
        // Get the ring buffer from the Disruptor to be used for publishing. 
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); 
 
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++)
        {
            bb.putLong(0, l);
            ringBuffer.publishEvent((event, sequence) -> event.setValue(bb.getLong(0)));
            try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }
	}
	
	public static void method1(){
		// Executor that will be used to construct new threads for consumers 
        ThreadFactory threadFactory =  new NamedThreadFactory("DisruptorDemo Thread",false);
        
        // The factory for the event 
        LongEventFactory factory = new LongEventFactory();
        
        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;
        
        // Construct the Disruptor 
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, threadFactory, ProducerType.SINGLE, new SleepingWaitStrategy());
        
        // Connect the handler 
        disruptor.handleEventsWithWorkerPool(new LongEventConsumer());
        
        // Start the Disruptor, starts all threads running 
        disruptor.start();
        
        // Get the ring buffer from the Disruptor to be used for publishing. 
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); 
 
        LongEventProducer producer = new LongEventProducer(ringBuffer); 
 
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) { 
        	
            bb.putLong(0, l); 
            producer.onData(bb); 
            try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
        } 
	}
}
